package cn.schoolwow.quickdao.test.module.business.ddl.flow;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.entity.common.TableColumn;
import cn.schoolwow.quickdao.domain.entity.create.CreateTableRequest;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.Validate;
import org.junit.Assert;

import java.util.List;

public class CreateTableProcessTestFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCreateTableRequest(flowContext);
        createUserTable(flowContext);
        insertUser(flowContext);
        updateUser(flowContext);
        deleteUser(flowContext);
    }

    @Override
    public String name() {
        return "创建指定表测试流程";
    }

    private void setCreateTableRequest(FlowContext flowContext){
        String name = flowContext.checkData("name", String.class);

        CreateTableRequest createTableRequest = new CreateTableRequest();
        createTableRequest.tableName = name;
        createTableRequest.comment = name;

        List<TableColumn> tableColumnList = QueryResource.newQuery(name+"定义", "module/business/ddl/表创建/"+name+"/"+name+"字段.json")
                .execute()
                .getAsInstanceList(TableColumn.class);
        TableColumn tableColumn = QueryInstanceList.newQuery(tableColumnList)
                .addQuery("column", "id")
                .execute()
                .checkOne();
        tableColumn.id = true;
        tableColumn.strategy = IdStrategy.AutoIncrement;
        flowContext.putCurrentFlowData("idColumnName", tableColumn.column);
        createTableRequest.columnList = tableColumnList;
        flowContext.putCurrentFlowInstanceData(createTableRequest);
    }

    private void createUserTable(FlowContext flowContext){
        CreateTableRequest createTableRequest = flowContext.checkInstanceData(CreateTableRequest.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        if(dao.hasTable(createTableRequest.tableName)){
            dao.dropTable(createTableRequest.tableName);
        }
        dao.createTable(createTableRequest);
        List<TableColumn> tableColumnList = dao.getTableColumnList(createTableRequest.tableName);
        Validate.notEmpty(tableColumnList, "表字段列表不能为空!表名:"+createTableRequest.tableName);
        Assert.assertEquals(createTableRequest.columnList.size(), tableColumnList.size());
    }

    private void insertUser(FlowContext flowContext){
        String name = flowContext.checkData("name", String.class);
        CreateTableRequest createTableRequest = flowContext.checkInstanceData(CreateTableRequest.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        JSONArray dataArray = QueryResource.newQuery(name+"数据", "module/business/ddl/表创建/"+name+"/"+name+"数据.json")
                .execute()
                .getAsJSONArray();
        dao.insert(createTableRequest.tableName, dataArray);
        for(int i=0;i<dataArray.size();i++){
            JSONObject data = dataArray.getJSONObject(i);
            String generateKeys = data.getString("generateKeys");
            Validate.notEmpty(generateKeys, "自增id未生成");
            data.put("id", generateKeys);
        }
        flowContext.putCurrentFlowData("dataArray", dataArray);
    }

    private void updateUser(FlowContext flowContext){
        JSONObject updateData = flowContext.checkData("updateData", JSONObject.class);
        JSONArray dataArray = flowContext.checkData("dataArray", JSONArray.class);
        CreateTableRequest createTableRequest = flowContext.checkInstanceData(CreateTableRequest.class);
        String idColumnName = flowContext.checkData("idColumnName", String.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        for(int i=0;i<dataArray.size();i++){
            JSONObject data = dataArray.getJSONObject(i);
            data.putAll(updateData);
        }
        dao.uniqueFieldNames(idColumnName).update(createTableRequest.tableName, dataArray);
    }

    private void deleteUser(FlowContext flowContext){
        JSONArray dataArray = flowContext.checkData("dataArray", JSONArray.class);
        CreateTableRequest createTableRequest = flowContext.checkInstanceData(CreateTableRequest.class);
        String idColumnName = flowContext.checkData("idColumnName", String.class);
        DAO dao = flowContext.checkInstanceData(DAO.class);

        dao.uniqueFieldNames(idColumnName).delete(createTableRequest.tableName, dataArray.getJSONObject(0));
    }
}
